package com.jhh.pay.heli.service.channel;

import com.alibaba.fastjson.JSONObject;
import com.jhh.pay.channel.pojo.BankInfo;
import com.jhh.pay.channel.pojo.RequestBean;
import com.jhh.pay.channel.pojo.ResponseBean;
import com.jhh.pay.channel.pojo.Structure;
import com.jhh.pay.channel.service.PayService;
import com.jhh.pay.heli.HelipayApplication;
import com.jhh.pay.heli.bean.HttpWrapper;
import com.jhh.pay.heli.bean.pay.OrderPayStateHto;
import com.jhh.pay.heli.bean.pay.PayForPersonHto;
import com.jhh.pay.heli.bean.pay.SinglePayForPersonResult;
import com.jhh.pay.heli.bean.withhold.SingleWithholdResult;
import com.jhh.pay.heli.bean.withhold.WithholdHto;
import com.jhh.pay.heli.bean.sign.PaySign;
import com.jhh.pay.heli.bean.sign.WithholdSign;
import com.jhh.pay.heli.constant.BaseConfig;
import com.jhh.pay.heli.constant.Company;
import com.jhh.pay.heli.constant.Constant;
import com.jhh.pay.heli.service.HeLiPayService;
import com.jhh.pay.heli.service.impl.HeLiPayServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by zhangqi on 2017/12/4.
 */
@Service
public class ChannelPayServiceImpl implements PayService {
    //TODO
    private final Logger mLogger = LoggerFactory.getLogger(HeLiPayServiceImpl.class);

    @Autowired
    BaseConfig mBaseConfig;

    @Autowired
    HeLiPayService mHeLiPayService;

    @Override
    public String getChannelName() {
        return mBaseConfig.getChannelName();
    }

    @Override
    public ResponseBean pay(RequestBean request) {
        /**
         * 封装参数
         */
        //银行卡信息
        BankInfo bankInfo = request.getBankInfo();

        PayForPersonHto forPerson = new PayForPersonHto();
        forPerson.setP1_bizType(Company.BIZTYPE_PAY);
        forPerson.setP2_orderId(request.getOrderNo());
        forPerson.setP3_customerNumber(Company.COMPANY_CODE);
        forPerson.setP4_amount(String.valueOf(request.getMoney()));
        forPerson.setP5_bankCode(bankInfo.getBankCard());
        forPerson.setP6_bankAccountNo(bankInfo.getBankNo());
        forPerson.setP7_bankAccountName(bankInfo.getPersonalName());
        forPerson.setP8_biz(Company.BIZ);
        forPerson.setP9_bankUnionCode("");
        forPerson.setP10_feeType(Company.FEETYPE);
        forPerson.setP11_urgency(Company.URGENCY);
        forPerson.setP12_summary("单笔代付");

        /**
         * 需要的签名密钥信息
         */
        PaySign paySign = new PaySign();
        paySign.setMD5_KEY(Constant.MD5_PAY_KEY);
        paySign.setRSA_PRIVATE(Constant.RSA_PRIVATE);

        HttpWrapper<SinglePayForPersonResult> wrapper = mHeLiPayService.payForPerson(forPerson,paySign);

        ResponseBean responseBean = new ResponseBean();
        if(Constant.SUCCESS == wrapper.getCode()){

        }
        return responseBean;
    }

    @Override
    public ResponseBean deduct(RequestBean request) {

        /**
         * 封装 代扣参数
         */
        BankInfo bankInfo = request.getBankInfo();
        ResponseBean responseBean = new ResponseBean();

        WithholdHto withholdHto = new WithholdHto();
        withholdHto.setP1_bizType(Company.BIZTYPE_WITHHOLD);
        withholdHto.setP2_customerNumber(Company.COMPANY_CODE);
        withholdHto.setP3_orderId(request.getOrderNo());
        withholdHto.setP4_timestamp(HelipayApplication.sSimpleDateFormat.format(new Date()));
        withholdHto.setP5_payerName(bankInfo.getPersonalName());
        withholdHto.setP6_idCardType(Constant.IDCARD_TYPE);
        withholdHto.setP7_idCardNo("320611199404071815");
        withholdHto.setP8_cardNo(bankInfo.getBankNo());
        withholdHto.setP9_phone("13270665702");
        withholdHto.setP10_currency(Constant.CURRENCY_TYPE);
        withholdHto.setP11_orderAmount(String.valueOf(request.getMoney()));
        withholdHto.setP12_goodsCatalog(Constant.GOODS_TYPE);
        withholdHto.setP13_goodsNam("苹果");
        withholdHto.setP14_goodsDesc("苹果");
        withholdHto.setP15_terminalType(Constant.TERMINAL_TYPE);
        withholdHto.setP16_terminalId("zhangqi199404071815");
        withholdHto.setP17_orderIp("106.114.223.93");
        withholdHto.setP18_serverCallbackUrl("http://106.14.12.184:8032/callback/withhold");

        WithholdSign withholdSign = new WithholdSign();
        withholdSign.setDES3_KEY(Constant.DES3_WITHHOLD_KEY);
        withholdSign.setMD5_KEY(Constant.MD5_WITHHOLD_KEY);
        HttpWrapper<SingleWithholdResult> wrapper = mHeLiPayService.withholdSingle(withholdHto,withholdSign);

        return responseBean;
    }

    /**
     * 代扣回调参数解析
     * @param request
     * @return
     */
    @Override
    public ResponseBean callbackResolve(RequestBean request) {


        String localResult = "{\"rt10_onlineCardType\":\"DEBIT\",\"rt11_completeDate\":\"2017-12-07 17:12:48\",\"rt12_cardAfterFour\":\"4261\",\"rt13_orderStatus\":\"SUCCESS\",\"rt1_bizType\":\"Withhold\",\"rt2_retCode\":\"0000\",\"rt3_retMsg\":\"成功\",\"rt4_customerNumber\":\"C1800000002\",\"rt5_orderId\":\"zhangqi00001119940487\",\"rt6_serialNumber\":\"WITHHOLD1712071712458ZUA\",\"rt7_orderAmount\":\"0.51\",\"rt8_bankName\":\"中国建设银行\",\"rt9_bankCode\":\"CCB\",\"sign\":\"7b39c03a1cb33d8310b981654732e705\"}";

        SingleWithholdResult result = JSONObject.parseObject(localResult,SingleWithholdResult.class);

        WithholdSign sign = new WithholdSign();
        sign.setMD5_KEY(Constant.MD5_WITHHOLD_KEY);

        mHeLiPayService.WithholdCallBackResult(result,sign);
        return null;
    }

    /**
     * 查询代付订单状态
     * @param request
     * @return
     */
    @Override
    public ResponseBean state(RequestBean request) {
        OrderPayStateHto orderStateHto = new OrderPayStateHto();
        orderStateHto.setP2_orderId(request.getOrderNo());
        orderStateHto.setP1_bizType(Company.BIZTYPE_PAY_QUERY);
        orderStateHto.setP3_customerNumber(Company.COMPANY_CODE);
        /**
         * 需要的签名密钥信息
         */
        PaySign paySign = new PaySign();
        paySign.setMD5_KEY(Constant.MD5_PAY_KEY);
        paySign.setRSA_PRIVATE(Constant.RSA_PRIVATE);

        mLogger.info(paySign.getMD5_KEY());
        mHeLiPayService.findPayOrderState(orderStateHto,paySign);
        return null;
    }

	@Override
	public Structure structure(RequestBean request) {
		// TODO Auto-generated method stub
		return null;
	}
}
